package javaTest02.exercise_3;

import javaTest02.utils.Person;
import javaTest02.utils.SettablePerson;
import javaTest02.utils.LogMethodInvoke;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;

public class DynamicProxyTest {//动态代理测试
    public static void main(String[] args) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person person=new Person();
        String outFile = "src/javaTest02/exercise_3/out/log.txt";

        //第一种
        InvocationHandler handler =new DynamicProxy(person,outFile);//实例化自定义的处理器
        Class<?> proxyClass = Proxy.getProxyClass(Person.class.getClassLoader(), SettablePerson.class);//获取生成的代理类的class对象
        SettablePerson DynamicPersonProxy1=(SettablePerson) proxyClass.getConstructor(InvocationHandler.class).newInstance(handler);//通过反射实例化代理类

        //更简洁的方式
        SettablePerson DynamicPersonProxy=(SettablePerson) Proxy.newProxyInstance(
                Person.class.getClassLoader(),
                new Class[]{SettablePerson.class},
                new DynamicProxy(person,outFile)
        );//生成代理实例

        LogMethodInvoke.generateProxyClassFile("DynamicPersonProxy",Person.class);//生成代理实例的.class文件

        DynamicPersonProxy.setName("派大星");
        DynamicPersonProxy.setAge(21);
        DynamicPersonProxy.setGender("男");
        DynamicPersonProxy.setHeight(230);
        DynamicPersonProxy.setWeight(200);
        DynamicPersonProxy.setAddress("比基尼海滩");

    }

}
